package com.lear.njava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lear.njava.mapper.ClazzMapper;
import com.lear.njava.pojo.Clazz;
import com.lear.njava.service.ClazzService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Override
    public IPage<Clazz> getClazzsByOpr(Page<Clazz> page, Clazz clazz) {
        // 创建QueryWrapper
        QueryWrapper<Clazz> queryWrapper = new QueryWrapper<Clazz>();
        // 获取年级名
        String gradeName = clazz.getGradeName();
        // 如果年级名不为空，拼接查询条件
        if (!StringUtils.isEmpty(gradeName)) { // !!!!踩坑，StringUtils.isEmpty(gradeName)，不是gradeName.isEmpty()
            queryWrapper.like("grade_name", gradeName);
        }
        // 获取班级名
        String name = clazz.getName();
        // 如果班级名不为空，拼接查询条件
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByDesc("id"); // 按照id降序排序
        // 通过baseMapper进行查询
        Page<Clazz> clazzPage = baseMapper.selectPage(page, queryWrapper);
        return clazzPage;
    }

    @Override
    public List<Clazz> getClazzs() {
        List<Clazz> clazzes = baseMapper.selectList(null);
        return clazzes;
    }
}
